/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.MuridDAO;
import com.ivanbiz.dao.impl.DaftarKelasDAOImpl;
import com.ivanbiz.dao.impl.MuridDAOImpl;
import com.ivanbiz.model.DaftarKelas;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.model.Murid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author iw20149x
 */
public class MuridService extends SelectorComposer<Component> {

    MuridDAO muridDAO;
    @Wire
    Listbox listMurid;
    @Wire
    Window winMurid;
    Kelas kelas;
    List<DaftarKelas> listDaftarKelas;
    DaftarKelasDAO daftarKelasDAO;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        kelas = (Kelas) Executions.getCurrent().getArg().get("kelas");
        listDaftarKelas = (List<DaftarKelas>) Executions.getCurrent().getArg().get("loadDaftarKelas");
        daftarKelasDAO = new DaftarKelasDAOImpl();
        muridDAO = new MuridDAOImpl();
        loadMurid();
        if (Sessions.getCurrent().getAttribute("pengguna") == null) {
            Messagebox.show("Illegal Akses!!!");
            Executions.sendRedirect("index.zul");
        }
    }

    public void loadMurid() {
        try {
            listMurid.getItems().clear();
            int no = 0;
            int x = 0;
            List<Murid> loadMurid = muridDAO.getAll(Murid.class);
            for (Murid murid : loadMurid) {
                no += 1;
                Listitem item = new Listitem();
                item.setValue(murid);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(murid.getNama()));
                item.appendChild(new Listcell(murid.getBank().getNama()));
                item.appendChild(new Listcell(murid.getHandphone()));
                item.appendChild(new Listcell(murid.getEmail()));
                item.appendChild(new Listcell(murid.getAlamat()));
                listMurid.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(PengajarService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#tambah;")
    public void tambah() {
        Window win = (Window) Executions.createComponents("/muridAction.zul", null, null);
        win.doModal();
        loadMurid();
    }

    @Listen("onClick=#ubah;")
    public void ubah() {
        if (listMurid.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan di ubah");
        } else {
            Murid murid = listMurid.getSelectedItem().getValue();
            Map map = new HashMap();
            map.put("murid", murid);
            Window win = (Window) Executions.createComponents("/muridAction.zul", null, map);
            win.setTitle("Ubah Murid");
            win.doModal();
            loadMurid();
        }
    }

    @Listen("onClick=#hapus;")
    public void hapus() throws Exception {
        if (listMurid.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan di hapus");
        } else {
            Murid murid = listMurid.getSelectedItem().getValue();
            muridDAO.delete(murid);
            loadMurid();
        }
    }

    @Listen("onDoubleClick=#listMurid;")
    public void pilihMurid() throws Exception {
        if (listDaftarKelas != null) {
            Murid murid = listMurid.getSelectedItem().getValue();
            for (DaftarKelas daftarKelass : listDaftarKelas) {
                if (daftarKelass.getMurid().getId() == murid.getId()) {
                    Messagebox.show("Murid sudah ada pada Jadwal Kelas");
                    return;
                }
            }
            DaftarKelas daftarKelas = new DaftarKelas();
            daftarKelas.setKelas(kelas);
            daftarKelas.setMurid(murid);
            daftarKelas.setTransactionReference(kelas.getTransactionReference());
            daftarKelas.setStatus("1");
            daftarKelas.setKehadiran("1");
            daftarKelas.setKeterangan("");
            daftarKelas.setKehadiran2("1");
            daftarKelas.setKeterangan2("");
            daftarKelas.setUjian(null);
            daftarKelasDAO.save(daftarKelas);
            winMurid.detach();
        }
    }
}
